<template>
	<view>
		<Navbar :backUrl="backUrl"></Navbar>
		<view class="u-m-20">
			<view class="u-card-box u-m-b-20" v-for="item in orderList" :key="item.uuid">
				<view class="u-flex u-row-between u-font-30">
					<view class="u-flex-1 u-font-bold u-ellipsis">{{item.title}}</view>
					<view class="u-color-red u-font-bold">{{item.price}}元</view>
				</view>
				<view class="u-m-t-20 u-flex u-row-between u-font-26">
					<view class="u-color-gray">{{item.hd_time}}</view>
					<!-- is_invoice 1待开票  2开票成功  3开票失败 -->
					<view class="apply_btn u-back-linear" @click="onApplyOrder(item)" v-if="item.is_invoice == 1">申请开票</view>
					<view class="apply_btn u-back-gray" @click="onOpenOrder(item)" v-else-if="item.is_invoice == 2">开票中</view>
					<view class="apply_btn u-back-darkBlue" @click="onOpenOrder(item)" v-else-if="item.is_invoice == 3">查看开票</view>
					<view class="apply_btn u-back-red" @click="onApplyOrder(item)" v-else-if="item.is_invoice == 4">开票失败，重新申请</view>
					<view class="apply_btn u-back-gray" @click="onOpenOrder(item)" v-else-if="item.is_invoice == 5">红冲中</view>
					<view class="apply_btn u-back-darkBlue" @click="onOpenOrder(item)" v-else-if="item.is_invoice == 6">已红冲</view>
					<view class="apply_btn u-back-red" @click="onOpenOrder(item)" v-else-if="item.is_invoice == 7">红冲失败</view>
				</view>
			</view>
		</view>
		<Loadmore :status="status"></Loadmore>

		<view style="height: 200rpx;"></view>

		<Bottom :isReturn="true" :backUrl="'mine'"></Bottom>
	</view>
</template>

<script>
	import Navbar from '@/components/Navbar.vue'
	import Loadmore from '@/components/Loadmore.vue'
	import Bottom from '@/components/Bottom.vue'
	import reachBottom from '@/mixins/reachBottom.js'
	import {
		userOrderList,
		getConfigInfo 
	} from '@/network/api.js'
	import { generateUUID } from '@/utils/index.js'
	export default {
		mixins: [reachBottom],
		components: {
			Navbar,
			Loadmore,
			Bottom
		},
		data() {
			return {
				orderList: []
			}
		},
		onLoad() {
			this.onGetOrderList()
		},
		mounted() {
			let _this = this
			uni.$on('update', function() {
				_this.page = 1
				_this.orderList.length = 0
				_this.status = 'loadmore'
				_this.onGetOrderList()
			})
		},
		onUnload() {
			uni.$off('update')
		},
		methods: {
			onGetOrderList() {
				const params = {
					user_id: uni.getStorageSync("user_id"),
					page: this.page,
					limit: this.pageSize
				}
				userOrderList(params).then(res => {
					if (res.data.list.length > 0) {
						res.data.list.map(item => {
							if(item.invoice){
								switch(item.invoice.hongchong_status){
									case 1:
									    item.is_invoice = 5
										break;
									case 2:
									    item.is_invoice = 6
										break;
									case 3:
									    item.is_invoice = 7
										break
								}
							}
							item.uuid = generateUUID()
							this.orderList.push(item)
						})
					}
					if (res.data.list.length < 10) this.status = 'nomore'
				})
			},
			onApplyOrder(val) {
				uni.navigateTo({
					url: "/operate/orderForm/orderForm?data=" + JSON.stringify(val)
				})
			},
			onOpenOrder(val) {
				let file = ''
				if(val.tea_invoice_callback_red && val.tea_invoice_callback_red.pdfUrl){
					file = val.tea_invoice_callback_red.pdfUrl
				}else if(val.tea_invoice_callback && val.tea_invoice_callback.pdfUrl ){
					file = val.tea_invoice_callback.pdfUrl
				}
				if(file){
					wx.downloadFile({
						url: file,
						header: {
							"Content-Type": "application/pdf;charset=UTF-8"
						},
						success: function(res) {
							if (res.statusCode == 200) {
								const filePath = res.tempFilePath;
								uni.showLoading({
									title: '正在打开',
									mask: true
								})
								wx.openDocument({
									filePath: filePath,
									fileType: 'pdf',
									showMenu: true,
									success: function(res) {
										uni.hideLoading()
									},
									fail: function(err) {
										uni.hideLoading()
									}
								});
							}
						},
						fail: function(err) {
							uni.hideLoading()
						}
					});
				}
			},
			loadData() {
				if (this.status == 'nomore') return
				this.page++
				this.status = 'loading'
				this.onGetOrderList()
			}
		}
	}
</script>

<style scoped>
	.apply_btn {
		color: #fff;
		padding: 13rpx 40rpx;
		border-radius: 50rpx;
	}
</style>